// packageA/groupAfterDetails/index.js
import moment from 'moment'
import Dialog from '@vant/weapp/dialog/dialog';
import Toast from '@vant/weapp/toast/toast';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        id: '',
        resourceURL: wx.api.host + '/',
        record: {},
        product: [],
        after_sales: {},
        order: {},
        quantity: '',
        detailinfo: [],
        shopinfo: {},
        status: {
            0: '全部',
            1: '待付款',
            2: '待发货',
            3: '待收货',
            4: '已完成',
            5: '已取消',
            10: '退款中',
            7: '申请退货',
            6: '退款成功',
        }, // 订单状态
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            id: options.id
        })
        this.getOrderDetail()

    },

     // 支付
     payOrder(e) {
        // console.log(e.currentTarget.dataset.id);
        let order_id = e.currentTarget.dataset.id
        wx.http.post(wx.api.tobepaid, {
            order_id: order_id,
            pay_end: 2
        }).then(res => {
            let {
                timeStamp,
                nonceStr,
                package: pa,
                signType,
                paySign
            } = res.data.data
            console.log(res);
            wx.requestPayment({
                timeStamp,
                nonceStr,
                package: pa,
                signType,
                paySign,
                success:(res)=> {
                    wx.showToast({
                      title:'支付成功',
                      icon:'none'
                    })
                    this.setData({
                        list:[],
                        page:1
                    },this.getOrderList)
                },
                fail:(err)=> {
                    wx.showToast({
                        title:'支付失败',
                        icon:'none'
                    })
                    // this.getOrderList()
                    // console.error(err);
                }
            })
        })
    },
    // 撤回订单
    cancelOrder(e) {
        let id = e.currentTarget.dataset.id
        Dialog.confirm({
            title: '提示',
            message: '确认撤回订单吗？',
        }).then(() => {
            wx.http.post(wx.api.userwithdrawal, {
                a_s_id: id,
            }).then(res => {
                if (res.code == 1) {
                    Toast.success('已撤回订单')
                    setTimeout(() => {
                        this.setData({
                            page: 1,
                            list: []
                        })
                        this.getOrderList()
                    }, 1000);
                }
            })
        }).catch(() => {
            // on cancel
        });
    },

    onClipboard(e) {
        wx.setClipboardData({
            data: e.currentTarget.dataset.value,
        })
    },

    // 申请售后
    useraftersales(e){
        wx.navigateTo({
            url: '../AfterSales/index?order_id=' + e.target.dataset.id,
        })
    },
    // 评价
    appraise(event) {
        let detail_id = event.target.dataset.id

        // console.log(detail_id,this.data.order.order_id);
        wx.navigateTo({
            url: '/packageB/comments/oneComment/index?detail_id=' + detail_id + '&order_id=' + this.data.order.order_id
        })
    },
    // 联系客服
    remindDelivery(e) {
        let shop_id = e.currentTarget.dataset.id
        wx.navigateTo({
            url: '/pages/chat/index?shop_id' + shop_id,
        })
    },
 //提醒发货
 remindOrder(e) {
    let id = e.currentTarget.dataset.id
    Dialog.confirm({
        title: '提示',
        message: '确认提醒发货吗？',
    }).then(() => {
        wx.http.post(wx.api.userremind, {
            order_id: id,
        }).then(res => {
            if (res.code == 1) {
                setTimeout(() => {
                    this.setData({
                        page: 1,
                        list: []
                    })
                    this.getOrderList()
                }, 1000);
            }
        })
    }).catch(() => {
        // on cancel
    });
},
    // 查看物流
    logistics(e) {
        let id = e.currentTarget.dataset.id
        wx.navigateTo({
            url: '../logistics/index?id=' + id,
        })
        // console.log(id);
    },
    //复制
    copy(phone) {
        wx.setClipboardData({ //设置系统剪贴板的内容
            data: phone,
            success(res) {
                wx.getClipboardData({ // 获取系统剪贴板的内容
                    success(res) {
                        wx.showToast({
                            title: '复制成功',
                        })
                    }
                })
            }
        })
    },
    // 详情
    getOrderDetail() {
        wx.http.get(wx.api.getorderdetail, {
            order_id: this.data.id
        }).then(res => {
            if (res.code == 1) {
                // console.log(res);
                this.setData({
                    order: res.data.order,
                    after_sales: res.data.order.after_sales,
                    product: res.data.productinfo,
                    detailinfo: res.data.productinfo[0].detailinfo,
                    shopinfo: res.data.productinfo[0].shopinfo,
                    'order.order_time': moment(Number(res.data.order.order_time) * 1000).format('YYYY-MM-DD HH:mm'),
                    'order.pay_time': moment(Number(res.data.order.pay_time) * 1000).format('YYYY-MM-DD HH:mm'),
                    'after_sales.update_time': res.data.order.after_sales ? moment(Number(res.data.order.after_sales.update_time) * 1000).format('YYYY-MM-DD HH:mm') : ''

                })

            }
        })
    },

     // 加入购物车
     Again(i) {
         let children = i.currentTarget.dataset.i
        //  console.log(children);
         Promise.all(children.map(t => {
            return wx.http.post(wx.api.usereditcart, {
                quantity: t.quantity, //商品数量
                stock_id: t.p_s_id, //商品规格
                product_id: t.product_id //商品id
            })
        })).then(res => {
            // console.log(res);
            const cardid = res.map(t => t.data).join(',')
            console.log(cardid);
            wx.navigateTo({
                url: '/pages/settlement/index?cart_ids=' + cardid,
            })
        }).catch(err => {
            // console.error(err);
            wx.showToast({
                title: '支付失败',
                icon:'none'
            })
        })
    },
    // makePhoneCall(e) {
    //     wx.makePhoneCall({
    //         phoneNumber: e.currentTarget.dataset.phone,
    //     })
    // },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})